﻿
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataFileApplication_WPF.Commands;
using DataFileApplication_WPF.Models;
using DataFileApplication_WPF.Common;
using System.Windows;
using DataFileApplication_WPF.Extensions;
using Newtonsoft.Json.Linq;

namespace DataFileApplication_WPF.ViewModels
{
    public class EditValueListViewModel : NotifyPropertyChangedBase
    {
        public DelegateCommand AddValueCommand { get; set; }

        public DelegateCommand RemoveValueCommand { get; set; }

        //要添加的值
        private object _newValue;

        public object NewValue
        {
            get { return _newValue; }
            set
            {
                if (_newValue != value)
                {
                    //值有效性验证  
                    switch (DataGridItemStructForEdit.Type)
                    {
                        //case EnumDataType.Float:
                        //    IsValidValue = float.TryParse(value.ToString(), out float floatValue);
                        //    break;
                        case EnumDataType.Integer:
                            IsValidValue = int.TryParse(value.ToString(), out int intValue);
                            break;
                        case EnumDataType.String:
                            IsValidValue = !string.IsNullOrWhiteSpace(value?.ToString());
                            break;
                        case EnumDataType.Double:
                            IsValidValue = double.TryParse(value.ToString(), out double doubleValue);
                            break;
                        case EnumDataType.Boolean:
                            IsValidValue = bool.TryParse(value.ToString(), out bool boolValue);
                            break;
                    }

                    //值有效才触发
                    _newValue = value;
                    RaisePropertyChanged();
                }
            }
        }

        //listbox选中的值
        private object _selectedItem;
        public object SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                this.RaisePropertyChanged();
            }
        }

        private DataGridItemStruct dataGridItemStructForEdit;
        public DataGridItemStruct DataGridItemStructForEdit
        {
            get { return dataGridItemStructForEdit; }
            set
            {
                dataGridItemStructForEdit = value;
                this.RaisePropertyChanged();
            }
        }

        private DataGridItemStruct oriDataGridItemStruct;
        public DataGridItemStruct OriDataGridItemStruct
        {
            get { return oriDataGridItemStruct; }
            set
            {
                oriDataGridItemStruct = value;
                this.RaisePropertyChanged();
            }
        }
        // 新增  值有效性验证是否通过
        private bool _isValidValue;
        public bool IsValidValue
        {
            get { return _isValidValue; }
            set
            {
                _isValidValue = value;
                RaisePropertyChanged();
            }
        }

        public EditValueListViewModel(DataGridItemStruct dataGridItemStruct)
        {
            // OriDataGridItemStruct = dataGridItemStruct;
            DataGridItemStructForEdit = dataGridItemStruct;
            // DataGridItemStructForEdit = dataGridItemStruct?.Clone();

            //初始化时将值加入列表
            //if (DataGridItemStructForEdit.ValueList.Count == 0 || !DataGridItemStructForEdit.ValueList.Contains(DataGridItemStructForEdit.Value))
            //{
            //    DataGridItemStructForEdit.ValueList.Add(DataGridItemStructForEdit.Value);
            //}

            AddValueCommand = new DelegateCommand((e) =>
            {
                object value = null;
                //将数据格式转换
                switch (DataGridItemStructForEdit.Type)
                {
                    //case Extensions.EnumDataType.Float:
                    //    value = Convert.ToSingle(NewValue);
                    //    break;
                    case Extensions.EnumDataType.Integer:
                        value = Convert.ToInt32(NewValue);
                        break;
                    case Extensions.EnumDataType.String:
                        value = Convert.ToString(NewValue);
                        break;
                    case Extensions.EnumDataType.Double:
                        value = Convert.ToDouble(NewValue);
                        break;
                    case Extensions.EnumDataType.Boolean:
                        value = Convert.ToBoolean(NewValue);
                        break;
                    default:
                        break;
                }

                if (DataGridItemStructForEdit.ValueList.Contains(value))
                {
                    MessageBox.Show("列表中已存在该值！");
                    return;
                }
                DataGridItemStructForEdit.ValueList.Add(value);
            });

            RemoveValueCommand = new DelegateCommand((e) =>
            {
                DataGridItemStructForEdit.ValueList.Remove(SelectedItem);
            });

        }
    }
}
